﻿using System;
using System.Collections.Generic;

namespace VI.Fsc.OperatorGui.Controllers
{
    public class SectionTranslationException : Exception
    {
        public SectionTranslationException() : base("Section translation file SectionTranslation.xml is corrupt")
        {
        }
    }

    public class SectionTranslationDoubleKeysException : Exception
    {
        public SectionTranslationDoubleKeysException()
            : base("Section translation file SectionTranslation.xml has double keys")
        {
        }
    }

    public class SectionTranslation
    {
        public virtual bool IsAvailable { get { return true; } }

        private readonly IDictionary<string, string> dictionary;

        public SectionTranslation(IDictionary<string, string> dictionary)
        {
            this.dictionary = dictionary;
        }

        public string GetTranslation(string section)
        {
            string value;
            if (!dictionary.TryGetValue(section, out value))
                return string.Empty;

            return value;
        }

        public static SectionTranslation Create(SectionTranslationReader reader)
        {
            return reader.GetIsAvailable() ? new SectionTranslation(reader.Read()) : new NoSectionTranslation();
        }

        public class NoSectionTranslation : SectionTranslation
        {
            public override bool IsAvailable { get { return false; } }

            public NoSectionTranslation() : base(new Dictionary<string, string>(0)) { }
        }
    }
}